package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.CheckGroupMapper;
import org.dey.pojo.CheckGroup;
import org.dey.pojo.CheckItem;
import org.dey.pojo.PageVo;
import org.dey.pojo.Setmeal;
import org.dey.service.CheckGroupService;
import org.dey.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hg
 * @date 2022/3/15
 * @classname
 */
@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {


    @Autowired
    CheckGroupMapper checkGroupMapper;

    @Autowired
    SetmealService setmealService;

    @Override
    public PageVo<CheckGroup> checkGroupXx(CheckGroup checkGroup, int pageNum, int pagesize) {


        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<CheckGroup>();
        if(StringUtils.isNotBlank(checkGroup.getName())){
            queryWrapper.like("name",checkGroup.getName());
        }

        PageHelper.startPage(pageNum, pagesize);

        List<CheckGroup> list = this.list(queryWrapper);

        PageInfo<CheckGroup> pageInfo = new PageInfo<CheckGroup>(list);


        PageVo<CheckGroup> pageVo = new PageVo<CheckGroup>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return  pageVo;
    }

    @Override
    public Result zjCheckGroupXx(CheckGroup checkGroup) {

        //添加检查组时先查询数据库 检查组表最后一条数据 得到该检查组的编码

        Result result =null;

        CheckGroup checkGroup1 = checkGroupMapper.getCheckGroup();

        String strcode = "";


        if(checkGroup1.getCode()!=null&&checkGroup1.getCode()!=""){
            String code = (Integer.parseInt(checkGroup1.getCode())+1)+"";
            strcode = StringUtils.leftPad(code, 4, "0");

        }

        checkGroup.setCode(strcode);

        boolean save = this.save(checkGroup);

        if(save){
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return result;
    }

    @Override
    public Result scCheckGroupXx(int id) {

        Result result =null;

        boolean save = this.removeById(id);

        if(save){
            result = new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

        return result;
    }

    @Override
    public int xgCheckGroupXx(CheckGroup checkGroup) {
        boolean update = this.updateById(checkGroup);
        return update==true?1:0;
    }

    @Override
    public CheckGroup cxCheckGroupXxBYid(int id) {

        CheckGroup query = this.getById(id);
        return query;
    }

    @Override
    public int addjcztjjcx(int id, CheckItem[] checkItem) {

        int addjcztjjcx = 0;

        float jiagehe = 0;
        for (CheckItem item : checkItem) {
            Integer id1 = item.getId();
            jiagehe = jiagehe + item.getPrice();
         addjcztjjcx = checkGroupMapper.addjcztjjcx(id, id1);
        }
        //添加检查项成功改变检查组的价格
        if(addjcztjjcx>=0){
            UpdateWrapper<CheckGroup> updateWrapper = new UpdateWrapper<CheckGroup>();
            updateWrapper.eq("id",id).set("price",jiagehe);
            boolean update = this.update(updateWrapper);
            if(update){
                addjcztjjcx = 1;
            }
        }
        return addjcztjjcx;
    }

    @Override
    public List<CheckItem> kcjcx(int id) {
      List<CheckItem> list =  checkGroupMapper.kcjcx(id);
        return list;
    }

    @Override
    public List<CheckGroup> checkGroupXxqb(int id) {
        boolean update = false;
        //查询前先清空数据
        int xc = checkGroupMapper.xc(id);
        //清除数据后还原套餐价格为0
        if(xc>=0){
            UpdateWrapper<Setmeal> updateWrapper = new UpdateWrapper<Setmeal>();
            updateWrapper.eq("id",id).set("price",0);
            update = setmealService.update(updateWrapper);
        }

        List<CheckGroup> list = null;
        if(update){
            if(xc>=0){
                QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<CheckGroup>();

                queryWrapper.select("id","name","price");

                list = this.list(queryWrapper);
            }
        }
        return list;
    }
}
